package cn.gcyl.module.biz.dal.mysql.specialmeeting;

import java.time.LocalDateTime;
import java.util.*;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gcyl.framework.mybatis.core.mapper.BaseMapperX;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.dal.dataobject.specialmeeting.SpecialMeetingDO;
import cn.gcyl.module.biz.dal.dataobject.thirdpartyinformation.ThirdPartyInformationDO;
import org.apache.ibatis.annotations.Mapper;
import cn.gcyl.module.biz.controller.admin.specialmeeting.vo.*;

import static cn.gcyl.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

/**
 * 专场会议管理 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface SpecialMeetingMapper extends BaseMapperX<SpecialMeetingDO> {

    default PageResult<SpecialMeetingDO> selectPage(SpecialMeetingPageReqVO reqVO) {
        LambdaQueryWrapperX<SpecialMeetingDO> queryWrapper = new LambdaQueryWrapperX<SpecialMeetingDO>()
                .likeIfPresent(SpecialMeetingDO::getMeetingName, reqVO.getMeetingName())
                .eqIfPresent(SpecialMeetingDO::getMeetingSynopsis, reqVO.getMeetingSynopsis())
                .betweenIfPresent(SpecialMeetingDO::getStartTime, reqVO.getStartTime())
                .eqIfPresent(SpecialMeetingDO::getIsFreeMeeting, reqVO.getIsFreeMeeting())
                .eqIfPresent(SpecialMeetingDO::getMeetingPlanId, reqVO.getMeetingPlanId())
                .eqIfPresent(SpecialMeetingDO::getMeetingAddress, reqVO.getMeetingAddress())
                .eqIfPresent(SpecialMeetingDO::getSort, reqVO.getSort())
                .eqIfPresent(SpecialMeetingDO::getCover, reqVO.getCover())
                .eqIfPresent(SpecialMeetingDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(SpecialMeetingDO::getCarouselImage, reqVO.getCarouselImage())
                .eqIfPresent(SpecialMeetingDO::getDetails, reqVO.getDetails())
                .betweenIfPresent(SpecialMeetingDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SpecialMeetingDO::getId);

        // 根据活动状态查询
        if (reqVO.getActiveState() != null) {
            switch (reqVO.getActiveState()) {
                case 0: // 未开始
                    queryWrapper.ge(SpecialMeetingDO::getStartTime, LocalDateTime.now());
                    break;
                case 1: // 进行中
                    queryWrapper.le(SpecialMeetingDO::getStartTime, LocalDateTime.now())
                            .ge(SpecialMeetingDO::getEndTime, LocalDateTime.now());
                    break;
                case 2: // 已结束
                    queryWrapper.lt(SpecialMeetingDO::getEndTime, LocalDateTime.now());
                    break;
            }
        }

        return selectPage(reqVO, queryWrapper.orderByDesc(SpecialMeetingDO::getId));
    }

    default PageResult<SpecialMeetingDO> getAppSpecialMeetingPage(AppSpecialMeetingPageReqVO reqVO) {
        LambdaQueryWrapperX<SpecialMeetingDO> queryWrapper = new LambdaQueryWrapperX<SpecialMeetingDO>()
                .likeIfPresent(SpecialMeetingDO::getMeetingName, reqVO.getMeetingName());
        queryWrapper.likeIfPresent(SpecialMeetingDO::getMeetingAddress, reqVO.getArea());
        queryWrapper.likeIfPresent(SpecialMeetingDO::getMeetingName, reqVO.getColumns());
        // 支持标题的模糊匹配（LIKE）
        if (reqVO.getSubscriptionWord() != null && reqVO.getSubscriptionWord().length > 0) {
            queryWrapper.and(wrapper -> {
                for (String name : reqVO.getSubscriptionWord()) {
                    wrapper.or().like(SpecialMeetingDO::getMeetingName, name);
                }
            });
        }
        // 根据活动状态查询
        if (reqVO.getActiveState() != null) {
            switch (reqVO.getActiveState()) {
                case 0: // 未开始
                    queryWrapper.ge(SpecialMeetingDO::getStartTime, LocalDateTime.now());
                    break;
                case 1: // 进行中
                    queryWrapper.le(SpecialMeetingDO::getStartTime, LocalDateTime.now())
                            .ge(SpecialMeetingDO::getEndTime, LocalDateTime.now());
                    break;
                case 2: // 已结束
                    queryWrapper.lt(SpecialMeetingDO::getEndTime, LocalDateTime.now());
                    break;
            }
        }

        return selectPage(reqVO, queryWrapper.orderByDesc(SpecialMeetingDO::getIsTop).orderByDesc(SpecialMeetingDO::getEndTime).orderByAsc(SpecialMeetingDO::getSort));
    }

    default PageResult<SpecialMeetingDO> getAppSpecialMeetingList(AppSpecialMeetingListReqVO reqVO) {
        Long userId = getLoginUserId();
        LambdaQueryWrapperX<SpecialMeetingDO> queryWrapper = new LambdaQueryWrapperX<SpecialMeetingDO>()
                .likeIfPresent(SpecialMeetingDO::getMeetingAddress, reqVO.getArea());

        // 支持标题的模糊匹配（LIKE）
        if (reqVO.getMeetingName() != null && reqVO.getMeetingName().length > 0) {
            queryWrapper.and(wrapper -> {
                for (String name : reqVO.getMeetingName()) {
                    wrapper.or().like(SpecialMeetingDO::getMeetingName, name);
                }
            });
        }
        // 添加根据用户ID查询咨询用户IDs的条件
        if (userId != null) {
            queryWrapper.apply("FIND_IN_SET({0}, user_collect_ids)", userId);
        }
        queryWrapper.orderByAsc(SpecialMeetingDO::getSort);

        // 使用分页查询，传入reqVO参数
        return selectPage(reqVO, queryWrapper);
    }
}